package vp.remedies.dr.home.fragment;

import java.util.ArrayList;
import java.util.HashMap;

import vp.remedies.dr.home.R;
import vp.remedies.dr.home.activity.WebViewClientActivity;
import vp.remedies.dr.home.db.AndroidSQLiteHelper;
import vp.remedies.dr.home.so.AllRemediesWrapper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class AllRemediesFragment extends Fragment {

	private Activity activity;

	// List view
	private ListView lv;

	// Listview Adapter
	ArrayAdapter<String> adapter;

	// Search EditText
	EditText inputSearch;

	public static AllRemediesWrapper allRemediesWrapper = null;
	// ArrayList for Listview
	ArrayList<HashMap<String, String>> remedieList;

	public AllRemediesFragment(Activity activity) {
		this.activity = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.all_remedies, container,
				false);
		// Listview Data
		String remedieNames[] = { "Ayurveda", "remedies"};
		
		remedieNames = AllRemediesFragment.getAllRemediesWrapper().getNames();
		
		lv = (ListView) rootView.findViewById(R.id.all_remedies_list);
		inputSearch = (EditText) rootView.findViewById(R.id.inputSearch);

		// Adding items to listview
		adapter = new ArrayAdapter<String>(activity, R.layout.remedies_name,
				R.id.remedie, remedieNames);
		
		lv.setAdapter(adapter);

		
		/**
		 * Enabling Search Filter
		 * */
		inputSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence cs, int arg1, int arg2,
					int arg3) {
				// When user changed the Text
				adapter.getFilter().filter(cs);
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
			}
		});
		
		lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int arg2,
					long arg3) {
				// On selecting single track get song information
				Intent i = new Intent(activity, WebViewClientActivity.class);
				
				String remedie = ((TextView) view.findViewById(R.id.remedie)).getText().toString();
				Log.i("test", "adapterView::");
				String htmlPageName = allRemediesWrapper.getAllRemediesMap().get(remedie);
				// Toast.makeText(activity, htmlPageName, Toast.LENGTH_LONG).show();
				
				i.putExtra(WebViewClientActivity.HTML_PAGE_NAME , htmlPageName);
				startActivity(i);
			}
		});	
		return rootView;
	}

	public static AllRemediesWrapper getAllRemediesWrapper() {
		allRemediesWrapper = new AllRemediesWrapper();
		AndroidSQLiteHelper helper = AndroidSQLiteHelper.getInstance();
		Cursor cursor = helper.getCursorRaw(
				"SELECT  id , name , html, bookmark FROM remedies_en ", null);
		allRemediesWrapper.setIds(new int[cursor.getCount()]);
		allRemediesWrapper.setNames(new String[cursor.getCount()]);
		int count = 0;
		while (cursor.moveToNext()) {
			int id = cursor.getInt(0);
			String name = cursor.getString(1);
			String html = cursor.getString(2);
			int bookmark = cursor.getInt(3);
			allRemediesWrapper.getIds()[count] = id;
			allRemediesWrapper.getNames()[count] = name;
			allRemediesWrapper.getAllRemediesMap().put(name, html);
			count++;
		}
		return allRemediesWrapper;
	}

}
